package com.ridaizu.model.search;

import java.util.ArrayList;

import javax.faces.bean.SessionScoped;

import com.google.common.base.Strings;

@javax.faces.bean.ManagedBean(name = "inventorySearch")
@SessionScoped
public class InventorySearch extends SearchModel {

	private static final long serialVersionUID = 1L;

	@Override
	public String getSelectSql() {

		StringBuffer sb = new StringBuffer();
		sb.append("WHERE i.goods_id = g.id \n");

		conditionList = new ArrayList<Object>();

		if (!Strings.isNullOrEmpty(this.sortField)) {
			sb.append(String.format("ORDER BY %s %s \n", this.sortField, this.sortType.toString()));
		} else {
			sb.append("ORDER BY id DESC \n");
		}

		sb.append("LIMIT ?, ? ");
		conditionList.add(first);
		conditionList.add(pageSize);

		return sb.toString();
	}

	@Override
	public String getSelectCountSql() {
		StringBuffer sb = new StringBuffer();
		conditionList = new ArrayList<Object>();
		sb.append("WHERE 1=1 \n");

		conditionList = new ArrayList<Object>();

		return sb.toString();
	}

	public static String mergerDate(String year, String month, String day, boolean isStart) {
		StringBuffer str = new StringBuffer();

		if (Strings.isNullOrEmpty(year)) {
			return null;
		}

		String split1 = "-";
		String split2 = "0";
		String split3 = "01";
		String split4 = "12";
		String split5 = "31 23:59:59";

		str.append(year).append(split1);

		if (Strings.isNullOrEmpty(month)) {
			if (isStart) {
				str.append(split3).append(split1);
			} else {
				str.append(split4).append(split1);
			}
		} else {
			String tmp = month.length() == 2 ? month : split2 + month;
			str.append(tmp).append(split1);
		}
		if (Strings.isNullOrEmpty(day)) {
			if (isStart) {
				str.append(split3);
			} else {
				str.append(split5);
			}
		} else {
			String tmp = day.length() == 2 ? day : split2 + day;
			str.append(tmp + " 23:59:59");
		}

		return str.toString();
	}


}
